package edu.szu.huhanpeng.doodlingFight.controller.dataController.manageDone 
{
	import com.aka.tools.array.DeleteRepetition;
	import edu.szu.huhanpeng.doodlingFight.globalConst.GlobalConst;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessData;
	import edu.szu.huhanpeng.doodlingFight.interfaces.ISquareData;
	import edu.szu.huhanpeng.doodlingFight.model.ChessBoardProxy;
	import edu.szu.huhanpeng.doodlingFight.model.ChessGroupProxy;
	import edu.szu.huhanpeng.doodlingFight.model.ChessProxy;
	import edu.szu.huhanpeng.doodlingFight.model.data.ChessData;
	import edu.szu.huhanpeng.doodlingFight.view.ChessMediator;
	import edu.szu.huhanpeng.doodlingFight.view.components.ChessComponent;
	import flash.utils.Dictionary;
	import org.puremvc.as3.interfaces.IAsyncCommand;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.patterns.command.AsyncCommand;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * ...
	 * @author Huhanpeng
	 */
	public class WrFillEmptySquareDc extends SimpleCommand implements ICommand, INotifier 
	{
		private var _waitingChessVec:Vector.<IChessData>;		//显示上等待补充进棋盘的棋子
		private var _needUpdate:Vector.<IChessData>;			//需要更新显示位置的棋子
		
		private var _tofindFriend:Vector.<IChessData>;
		
		override public function execute(note:INotification):void 
		{
			
			_waitingChessVec = new Vector.<IChessData>();		//显示上等待补充进棋盘的棋子
			_needUpdate = new Vector.<IChessData>();			//需要更新显示位置的棋子
		
			_tofindFriend = new Vector.<IChessData>();
			
			
			var groupProxy:ChessGroupProxy = facade.retrieveProxy( ChessGroupProxy.NAME )　as ChessGroupProxy;
			var doneArr:Array = groupProxy.getDone();
			
			
			
			var vec:Vector.<IChessData> = new Vector.<IChessData>();
			var a:Vector.<IChessData>;
			for (var m:int = 0; m < doneArr.length; m++) 
			{
				a = doneArr[m] as Vector.<IChessData>;
				a.forEach(function (item:IChessData, index:int, v:Vector.<IChessData>):void { vec.push(item) } );
				//vec.push();
			}
			
			
			//找出每一列所有空缺的深度
			var dic:Dictionary = new Dictionary();	//列为key，深度为值
			var removeIndex:Vector.<int> = new Vector.<int>();
			var depth:int;
			var column:int;
			for (var j:int = 0; j < vec.length; j++) 
			{
				column = vec[j].square.column;
				depth = vec[j].square.row;
				if (dic[column] == null) 
				{
					dic[column] = new Vector.<int>();
					removeIndex.push(column);
				}
				(dic[column] as Vector.<int>).push(depth);
			}
			removeIndex.sort(Array.DESCENDING);
			
			
			//根据空缺移动棋子
			var chessBoardProxy:ChessBoardProxy = facade.retrieveProxy(ChessBoardProxy.NAME) as ChessBoardProxy;
			var removeVec:Vector.<int>;
			var columnVec:Vector.<ISquareData>;
			//对每列深度分别排序
			var lowLy:int;	
			var highLy:int;
			var squareA:ISquareData;
			var squareB:ISquareData;
			var chessA:IChessData;
			var chessB:IChessData;
			var distance:int;	//棋子每次移动的格数
			var num:int;		//此列移走棋子的数量
			while(removeIndex.length != 0)
			{
				column = removeIndex.pop();
				removeVec = dic[column] as Vector.<int>;
				removeVec.sort(Array.NUMERIC);
				DeleteRepetition.todo( Vector.<int> as Class, removeVec );
				num = removeVec.length;
				
				columnVec = chessBoardProxy.getColumn(column);
				
				distance = 1;
				while (true)
				{
					lowLy = removeVec.pop();
					
					if (removeVec.length != 0) 
					{
						highLy = removeVec.pop();
					}
					else
					{
						highLy = -1;
					}
					
					//chessB为移动的的棋子
					for (var i:int = lowLy-1; i > highLy ; i--) 
					{
						squareA = columnVec[i + distance] as ISquareData;
						chessA =( squareA.chessData != null ) ? squareA.chessData : chessB;		//为null就是移动过，chessB 为上次移开的棋子
						squareB = columnVec[i] as ISquareData;
						chessB = squareB.chessData;
						
						squareA.placeChess(chessB);
						
						//未完成：不需要重设friend的优化
						if ( false && chessB.type == chessA.type ) 
						{
							chessB.friend = chessA.friend;
							for (var q:int = 0; q < chessB.friend.length; q++) 
							{
								if (chessB.friend[q] != null) 
								{
									//重设对方的friend,相反方向的key相加为10
									chessB.friend[q].friend[Math.abs( 10 - q )] = chessB;
								}
							}
						}
						else
						{
							_tofindFriend.push( chessB );
						}
						_needUpdate.push( chessB );
					}
					
					if (highLy == -1) 
					{
						break;
					}
					else
					{
						removeVec.push(highLy);
						distance++;
					}
				}
				
				//创建新棋子填补num个空缺
				var chessData:IChessData;
				var chessComponent:ChessComponent;
				var square:ISquareData;
				for (var k:int = num - 1; k >= 0; k--) 
				{
					square  = columnVec[k] as ISquareData;
					chessData = new ChessData();
					facade.registerProxy(new ChessProxy(chessData));
					square.placeChess(chessData);
					
					chessData.type = Math.floor(Math.random() * GlobalConst.setting.CHESS_TYPE) + 1;
					chessData.updatePosData(square.row, square.column);
					chessComponent = new ChessComponent( chessData.type );
					chessComponent.name = chessData.name;
					//chessComponent.x = chessData.x;
					//chessComponent.y = chessData.y;
					chessComponent.addEvent();
					facade.registerMediator(new ChessMediator( chessComponent ));
					
					_tofindFriend.push( chessData );
					_waitingChessVec.push( chessData );
				}
			}
			
			var arr:Array = new Array();
			
			arr.push( _needUpdate );
			arr.push( _waitingChessVec );
			arr.push( _tofindFriend );
			
			note.setBody( arr );
		}
		
	}

}